﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace WpfApplication1
{
    [Serializable()]
    public class HighScore : ISerializable
    {
        public string Name { get; set; }
        public int Score { get; set; }

        public HighScore()
        {
        }

        public HighScore(SerializationInfo info, StreamingContext ctxt)
        {
            this.Name = (string)info.GetValue("Name", typeof(string));
            this.Score = (int)info.GetValue("Score", typeof(int));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Name", this.Name);
            info.AddValue("Score", this.Score);
        }


        private void SerializeObject(string filename, List<HighScore> objectToSerialize)
        {
            Stream stream = File.Open(filename, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, objectToSerialize);
            stream.Close();
        }

        private List<HighScore> DeSerializeObject(string filename)
        {
            List<HighScore> objectToSerialize;
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            objectToSerialize = (List<HighScore>)bFormatter.Deserialize(stream);
            stream.Close();
            return objectToSerialize;
        }


        private const string FileName = "HighScores.bin";

        public static List<HighScore> Read()
        {
            //if (!File.Exists("HighScores.bin"))
                return Generate();
        }

        public static void Write(List<HighScore> highScores)
        {
            
        }

        private static List<HighScore> Generate()
        {
            List<HighScore> l = new List<HighScore>();
            char alpha = Char.Parse("A"); 
            
            for (int i = 0; i < 10; i++)
            {
                l.Add(new HighScore() { Name = string.Format("{0}{1}{2}", alpha, alpha, alpha), Score = i * 100 });
                alpha++;
            }
            return l;
        }

    }
}
